package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.core.api.AdTableDelCmd;
import com.jackrain.nea.core.entity.AdTableModel;
import com.jackrain.nea.core.mapper.*;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdTableDelCmdImpl extends CommandAdapter implements AdTableDelCmd {

    @Autowired
    private AdTableMapper adTableMapper;
    @Autowired
    private DirectoryMapper directoryMapper;
    @Autowired
    private AdAliasTableMapper adAliasTableMapper;
    @Autowired
    private AdColumnMapper adColumnMapper;
    @Autowired
    private AdRefbyTableMapper adRefbyTableMapper;
    @Autowired
    private AdTableSqlMapper adTableSqlMapper;
    @Autowired
    private GroupPermMapper groupPermMapper;
    @Autowired
    private MapperDDL mapperDDL;
    @Autowired
    private AdRefbyTableDelCmdImpl adRefbyTableDelCmdImpl;

    @Override
    @Transactional
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);

        long objectId = data.getObjId();

        //JSONArray jar = data.getItemData("AD_REFBYTABLE", false);

        
        if (!data.isDeleteMainTable()) {
            return adRefbyTableDelCmdImpl.execute(session);
        }
        

        AdTableModel model = adTableMapper.selectByTableId(objectId);
        long realtabid = Tools.getLong(model.getRealtableId(), -1);
        String tabname = model.getName();
        
        //drop table
        if (realtabid < 0) {
            String ddl ="drop table "+ tabname +" cascade constraints";
            adTableSqlMapper.DDLLog(objectId,ddl);
            //mapperDDL.dropTable(tabname);
        }

        //remove all columns of this table
        adColumnMapper.deleteWithTable(objectId);
        adAliasTableMapper.delete(objectId);
        adRefbyTableMapper.delete(objectId);
        adTableSqlMapper.delete(objectId);


        

        //delete from directory if no other table refers to that dir
        long directoryid = Tools.getLong(adTableMapper.directoryId(objectId), -1);

        int pcount = adTableMapper.directoryCountNotWithTable(directoryid, objectId);

        //delete ad_table
        adTableMapper.delete(objectId); //需要先删除表才能删除安全目录 2017.11.30 from:zzj
        if (pcount == 0) {
            pcount = directoryMapper.countWithTable(directoryid, objectId);
            //if directory is right for this table
            if (pcount == 1) {
                //this directory if for this table solely
                groupPermMapper.delete(directoryid);
                directoryMapper.delete(directoryid);
            }

        }

        
        return data.success();
    }
}
